<?php
/**
 * FlickrPhotosetsConfig class file
 * @package FlickrPhotosets
 */

/**
 * Include styles.
 */
require_once 'styles.php';

/**
 * This class handles the configuration page.
 * 
 * @package FlickrPhotosets
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 7.10.15
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 * @since Class available since Release 2.0
 */
class FlickrPhotosetsConfig extends FlickrPhotosets {
	
	/**
	 * The messages to print out after the header.
	 * @var string
	 */
	private $messages;
	
	/**
	 * Requires that the page is in the frame and that the user has
	 * added the application. Initiates the functions that handle
	 * updates of the configuration.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function __construct() {
		parent::__construct();
		
		// To make this a canvas page, we should require the frame
		$this->facebook->require_frame();
		
		// The user must add the app to see the configuration page
		$this->uid = $this->facebook->require_add();
		
		// Now that we have the UID, let's get their configuration data
		if (!$this->get_info()) {
			$this->error(FlickrPhotosetsErrors::$database_error);
		}
		
		// Set up Flickr
		$this->init_flickr(false, false);
		
		// Get the user's information from Flickr, but only if they have authenticated
		if ($this->is_flickr_authorized()) {
			$this->flickr_user = $this->flickr->people_getInfo($this->nsid);
		}
		
		// Handle updates
		$this->handle_post();
		$this->handle_update();
	}
	
	/**
	 * Adds the FBML for an error message to the messages string.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $text The success message to add
	 */
	private function add_error_message($text) {
		static $e = 1;
		$this->messages .= '<div id="error'.$e.'"><fb:error message="'.$text.'"><a href="#" clicktohide="error'.$e.'">Hide</a></fb:error></div>';
		$e++;
	}
	
	/**
	 * Adds the FBML for a success message to the messages string.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $text The success message to add
	 */
	private function add_success_message($text) {
		static $i = 1;
		$this->messages .= '<div id="success'.$i.'"><fb:success message="'.$text.'"><a href="#" clicktohide="success'.$i.'">Hide</a></fb:success></div>';
		$i++;
	}
	
	/**
	 * Uses the post variables to update the user's preferences.
	 * Redirects to other pages as necessary. Validates information
	 * as necessary.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com
	 */
	public function handle_post() {
		
		/*
		Update privacy settings
		*/
		if (isset($_POST['fp_privacy']) and $_POST['fp_privacy'] == 'Set') {
			$this->config['privacy_filter'] = $_POST['privacy_filter'];
			$this->config['privacy_visibility'] = $_POST['privacy_visibility'];
			$this->config['privacy_name'] = (empty($_POST['privacy_name'])) ? $this->privacy_name['first'] : $this->privacy_name['full'];
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->add_success_message('You have successfully updated your privacy options.');
			}
		}
		/*
		Reset privacy settings to defaults
		*/
		else if (isset($_POST['fp_privacy']) and $_POST['fp_privacy'] == 'Reset') {
			$this->config['privacy_filter'] = $this->privacy_filter['public'];
			$this->config['privacy_visibility'] = $this->privacy_visibility['all'];
			$this->config['privacy_name'] = $this->privacy_name['first'];
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->add_success_message('You have successfully reset your privacy options to their default settings.');
			}
		}
		/*
		Update program features
		*/
		else if (isset($_POST['fp_features']) and $_POST['fp_features'] == 'Set') {
			
			// Figure out if the user changed the profile setting
			$profile_changed = ($this->config['enable_profile'] == $_POST['enable_profile']) ? false : true;
			
			$this->config['enable_profile'] = $_POST['enable_profile'];
			$this->config['enable_gallery'] = $_POST['enable_gallery'];
			$this->config['enable_gallery_photostream'] = $_POST['enable_gallery_photostream'];
			$this->config['enable_gallery_photosets'] = $_POST['enable_gallery_photosets'];
			$this->config['enable_gallery_geotagged'] = $_POST['enable_gallery_geotagged'];
			$this->config['enable_newsfeed'] = $_POST['enable_newsfeed'];
			
			// Disable the gallery if the user unchecked all of the gallery features
			if (($_POST['enable_gallery_photostream'] == 0) and ($_POST['enable_gallery_photosets'] == 0) and ($_POST['enable_gallery_geotagged'] == 0)) {
				$this->config['enable_gallery'] = 0;
			}
			
			// Save to the database and display a success message or redirect to update the profile
			if ($this->save_config()) {
				if ($profile_changed and $this->config['enable_profile']) {
					$this->facebook->redirect($this->app_url.'/update.php');
				}
				else if ($profile_changed and !$this->config['enable_profile']) {
					$this->facebook->redirect($this->app_url.'/update.php?remove');
				}
				$this->add_success_message('You have successfully enabled or disabled program features.');
			}
			
		}
		/*
		Reset features to default settings (enable all)
		*/
		else if (isset($_POST['fp_features']) and $_POST['fp_features'] == 'Reset') {
			
			// Figure out if the user changed the profile setting
			$profile_changed = ($this->config['enable_profile']) ? false : true;
			
			$this->config['enable_profile'] = 1;
			$this->config['enable_gallery'] = 1;
			$this->config['enable_gallery_photostream'] = 1;
			$this->config['enable_gallery_photosets'] = 1;
			$this->config['enable_gallery_geotagged'] = 1;
			$this->config['enable_newsfeed'] = 1;
			
			// Save to the database and display a success message or redirect to update the profile
			if ($this->save_config()) {
				if ($profile_changed) {
					$this->facebook->redirect($this->app_url.'/update.php');
				}
				$this->add_success_message('You have successfully reset the program features.');
			}
		}
		/*
		Update profile settings
		*/
		else if (isset($_POST['fp_profile']) and $_POST['fp_profile'] == 'Set') {
			$this->config['profile_display'] = $_POST['profile_display'];
			$this->config['profile_count'] = $_POST['profile_count'];
			$this->config['profile_tags'] = $_POST['profile_tags'];
			$this->config['profile_tagmode'] = $_POST['profile_tagmode'];
			$this->config['profile_sort'] = $_POST['profile_sort'];
			
			// Make sure the photo count is valid
			if ($this->config['profile_count'] > 30) {
				$this->config['profile_count'] = 30;
			}
			else if ($this->config['profile_count'] < 1) {
				$this->config['profile_count'] = 1;
			}
			
			// Save to the database and display a success message
			$this->save_config();
			$this->facebook->redirect($this->app_url.'/update.php');
		}
		/*
		Reset profile settings to defaults
		*/
		else if (isset($_POST['fp_profile']) and $_POST['fp_profile'] == 'Reset') {
			$this->config['profile_display'] = $this->profile_display['last'];
			$this->config['profile_count'] = $this->profile_count;
			$this->config['profile_tags'] = '';
			$this->config['profile_tagmode'] = $this->tagmode['any'];
			$this->config['profile_sort'] = $this->profile_sort['date_posted_desc'];
			
			// Save to the database and display a success message
			if ($this->save_config()) {
				$this->facebook->redirect($this->app_url.'/update.php');
			}
		}
		/*
		Fix profile box issues
		*/
		else if (isset($_POST['fp_problems']) and $_POST['fp_problems'] == 'Reset profile box') {
			// Reset FBML
			$this->set_profile_fbml();
			
			// Redirect to the update page
			$this->facebook->redirect($this->app_url.'/update.php');
		}
		
	}
	
	/**
	 * Handle get variables sent back from update.php.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function handle_update() {
		if (isset($_GET['profileupdated'])) {
			$this->add_success_message('You have successfully updated your profile options.');
		}
		else if (isset($_GET['profileremoved'])) {
			$this->add_success_message('You have successfully removed the profile feature.');
		}
	}
	
	/**
	 * Prints out the messages.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function print_messages() {
		echo $this->messages;
	}
	
	/**
	 * Saves the current configuration to the database ($config).
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return boolean True if the query was successful
	 */
	private function save_config() {
		return $this->db->save_config($this->config);
	}
	
}

?>